package com.yanq.liancar.fragment;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import com.vondear.rxtool.view.RxToast;
import com.yanq.liancar.R;
import com.yanq.liancar.adapter.ShopEvaluateListAdapter;
import com.yanq.liancar.base.BaseLYFragment;
import com.yanq.liancar.bean.OrderListBean;
import com.yanq.liancar.bean.ShopCommentBean;
import com.yanq.liancar.bean.ShopEvaluateBean;
import com.yanq.liancar.bean.UserBean;
import com.yanq.liancar.url.Config;
import com.yanq.liancar.util.LogUtils;

import java.util.ArrayList;
import java.util.List;
import butterknife.BindView;

public class ShopEvaluateFragment extends BaseLYFragment {

    @BindView(R.id.order_recyclerview)
    RecyclerView mRecyclerview;
    @BindView(R.id.smartrefreshlayout)
    SmartRefreshLayout mSmartRefreshLayout;

    private int page = 1;
    private int sid,type;
    private boolean isClear=true;
    private ShopEvaluateListAdapter adapter;
    private List<ShopCommentBean.DataBean> mList = new ArrayList<>();


    public static ShopEvaluateFragment newInstance(int sid,int type) {
        Bundle bundle = new Bundle();
        bundle.putInt("sid", sid);
        bundle.putInt("type", type);
        ShopEvaluateFragment pageFragment = new ShopEvaluateFragment();
        pageFragment.setArguments(bundle);
        return pageFragment;
    }

    @Override
    protected void onCreateViewLazy(Bundle savedInstanceState) {
        super.onCreateViewLazy(savedInstanceState);
        setContentView(R.layout.fragment_orderlist);
        sid = getArguments().getInt("sid");
        type = getArguments().getInt("type");
        initRecyclerview();
        initListener();
        getServiceOrderList();
    }

    private void initRecyclerview() {
        mRecyclerview.setNestedScrollingEnabled(false);
        mRecyclerview.setLayoutManager(new LinearLayoutManager(mActivity));
        adapter = new ShopEvaluateListAdapter(R.layout.item_small_b_shopevaluate, mList);
        mRecyclerview.setAdapter(adapter);
        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
//                Bundle bundle = new Bundle();
//                bundle.putInt("Id", mList.get(position).id);
//                bundle.putInt("auditStatus", auditStatus);
//                readyGo(ApplyDetailsActivity.class, bundle);
            }
        });
    }

    private void initListener() {
        mSmartRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                page = 1;
                isClear = true;
                getServiceOrderList();
                refreshlayout.finishRefresh();
            }
        });
        mSmartRefreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                page++;
                isClear = false;
                getServiceOrderList();
                refreshLayout.finishLoadMore();
            }

        });
    }
    //获取服务订单
    private void getServiceOrderList() {
        LogUtils.e("dasdasda",sid+"============"+type);
        OkGo.<String>post(Config.s + "llc/shop/showAllShopRemark")
                .tag(this)
                .params("sid",sid )
                .params("type", type)
                .params("page", page)
                .params("num", 5)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        Gson gson = new Gson();
                        ShopCommentBean shopCommentBean = gson.fromJson(response.body(), ShopCommentBean.class);
                        if (shopCommentBean.isIsSuccess()) {
                            if (isClear) {
                                mList.clear();
                            }
                            mList.addAll(shopCommentBean.getData());
                            adapter.setNewData(mList);
                        }
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        RxToast.error("服务器连接错误");
                    }
                });
    }
}
